package com.efast.dmom.mgmt.controller.les.pda;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.dmom.mgmt.bean.les.PdaResultTwOutbTaskDetailBean;
import com.efast.dmom.mgmt.bean.les.PdaTwOutbTaskHeaderDetailBean;
import com.efast.dmom.mgmt.entity.les.TwOutbTaskDetail;
import com.efast.dmom.mgmt.entity.les.TwOutbTaskHeader;
import com.efast.dmom.mgmt.service.les.TwOutbTaskDetailService;
import com.efast.dmom.mgmt.service.les.TwOutbTaskHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 拣货任务表 TW_OUTB_TASK_DETAIL。
 *
 * 扫描或者输入拣货任务单号，显示出这张单据所有信息，有多条的，显示下一条 按照需求编辑页面数据，点击确认，拣货完成
 * 
 * @author chlp
 */
@Controller
@RequestMapping(value = "/pdaTwOutbTaskDetail")
@Api(description = "pda->出库->7.5.3.2明细拣货/7.5.3.3跟踪号拣货")
public class PdaTwOutbTaskDetailController {

	private final TwOutbTaskHeaderService twOutbTaskHeaderService;
	private final TwOutbTaskDetailService twOutbTaskDetailService;

	@Autowired
	public PdaTwOutbTaskDetailController(TwOutbTaskHeaderService twOutbTaskHeaderService,
			TwOutbTaskDetailService twOutbTaskDetailService) {
		this.twOutbTaskHeaderService = twOutbTaskHeaderService;
		this.twOutbTaskDetailService = twOutbTaskDetailService;
	}

	/**
	 * 7.5.3.2明细拣货
	 * 
	 * 7.5.3.3跟踪号拣货 也是调用该接口
	 *
	 * e)实现某行取消拣货，单行变成新单，整单变成部分拣货
	 * 
	 * 实际捡货数量字段需要增加。可捡货数量需要减去。 实际捡货箱数字段需要增加。可捡货箱数需要减去。
	 * 
	 * @param pdaTwOutbTaskHeaderDetailBean
	 *            pdaTwOutbTaskHeaderDetailBean
	 */
	@RequestMapping(value = "/confirmPicking", method = RequestMethod.POST)
	@ApiOperation(value = "【pda 明细拣货】", notes = "拣货任务单号：pick_alloc_no和tw_outb_task_header_id")
	public @ResponseBody ResultBean confirmPicking(
			@RequestBody PdaTwOutbTaskHeaderDetailBean pdaTwOutbTaskHeaderDetailBean) {
		ResultBean result = new ResultBean(true, "'");
		twOutbTaskHeaderService.confirmPickingEdit(pdaTwOutbTaskHeaderDetailBean.getTwOutbTaskDetails());
		return result;
	}

	/**
	 * pda根据拣货任务单号查询数据
	 *
	 * @param twOutbTaskHeader
	 *            twOutbTaskHeader
	 */
	@RequestMapping(value = "/query")
	@ApiOperation(value = "【pda根据拣货任务单号查询数据】", notes = "拣货任务单号：pick_alloc_no")
	public @ResponseBody ResultBean cancelPicking(@RequestBody TwOutbTaskHeader twOutbTaskHeader) {
		ResultBean result = new ResultBean(true, "'");
		System.out.println();
		List<TwOutbTaskHeader> twOutbTaskHeaderList = twOutbTaskHeaderService.selectTwOutbTaskHeader(
				new Assist(Assist.andEq("pick_alloc_no", twOutbTaskHeader.getPickAllocNo())));
		if (twOutbTaskHeaderList.size() == 0) {
			throw new ServiceException("单号不存在");
		}
		List<PdaResultTwOutbTaskDetailBean> twOutbTaskDetailList = twOutbTaskDetailService.queryTaskDetailService(
				twOutbTaskHeaderList.get(0).getTwOutbTaskHeaderId());
		if (twOutbTaskDetailList.size() == 0) {
			throw new ServiceException("单号对应的明细没有数据");
		}
		result.setObject(twOutbTaskDetailList);
		return result;
	}

	/**
	 * 7.5.3.3跟踪号拣货
	 *
	 * 扫描或者输入零件包装流水号/入库跟踪号，显示出对应的信息 ，多条数据时，显示下一条；只有最后一条时，确认按钮才显示或可编辑
	 *
	 * 跟踪号拣货时，跟踪号和包装流水号不允许编辑；
	 * 
	 * @param twOutbTaskDetail
	 *            twOutbTaskDetail
	 */
	@RequestMapping(value = "/queryByNo")
	@ApiOperation(value = "【pda根据拣货跟踪号拣货查询数据(跟踪号拣货)】", notes = "请求参数：零件包装流水号/入库跟踪号")
	public @ResponseBody ResultBean queryByNo(@RequestBody TwOutbTaskDetail twOutbTaskDetail) {
		ResultBean result = new ResultBean(true, "'");
		List<TwOutbTaskHeader> twOutbTaskHeaderList = twOutbTaskHeaderService.selectTwOutbTaskHeader(
				new Assist(Assist.andEq("tw_outb_task_detail_id", twOutbTaskDetail.getTwOutbTaskDetailId())));
		if (twOutbTaskHeaderList.size() == 0) {
			throw new ServiceException("单号不存在");
		}
		Assist assist = new Assist();
		if (twOutbTaskDetail.getPartPackSerial() != null) {
			assist.setRequires(Assist.andEq("part_pack_serial", twOutbTaskDetail.getPartPackSerial()));
		}
		if (twOutbTaskDetail.getInbSerialNo() != null) {
			assist.setRequires(Assist.andEq("inb_serial_no", twOutbTaskDetail.getInbSerialNo()));
		}
		if (twOutbTaskDetail.getPartPackSerial() == null && twOutbTaskDetail.getInbSerialNo() == null) {
			throw new ServiceException("请输入查询条件");
		}
		List<TwOutbTaskDetail> twOutbTaskDetailList1 = twOutbTaskDetailService.selectTwOutbTaskDetail(assist);
		List<PdaResultTwOutbTaskDetailBean> twOutbTaskDetailList = twOutbTaskDetailService.queryTaskDetailService(
				twOutbTaskDetailList1.get(0).getTwOutbTaskHeaderId());
		if (twOutbTaskDetailList.size() == 0) {
			throw new ServiceException("单号对应的明细没有数据");
		}
		result.setObject(twOutbTaskDetailList);
		return result;
	}

}
